package cn.com.cetccst.messagecenter;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.text.TextUtils;

import androidx.core.app.NotificationCompat;

import cn.com.cetccst.messagecenter.inboxmessage.InboxMessageActivity;

public class MessageUtil {

    public static final String INBOX_CHANNEL = "inbox";

    public static String subString(String string, int length) {
        if (!TextUtils.isEmpty(string) && string.length() > length) {
            return string.substring(0, length);
        }
        return string;
    }

    public static String getMyAppName(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
        }
        return packageInfo != null ? packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString() : "";
    }


    public static void newMessageNotification(Context context, int count) {
        String message = context.getString(R.string.im_x_unread_msgs, count);

        String title = context.getString(R.string.im_new_msg);
        Intent intent = new Intent();
        intent.setClass(context, InboxMessageActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    INBOX_CHANNEL,
                    INBOX_CHANNEL,
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(false);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = new NotificationCompat.Builder(context, INBOX_CHANNEL)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_unread_indicator)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setNumber(count)
                .setAutoCancel(true)
                .build();

        notificationManager.notify(INBOX_CHANNEL.hashCode(), notification);
    }
}
